﻿using System;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
using System.Web.Caching;

using LTJ.Common;
using LTJ.Data;
using LTJ.Config;
using LTJ.Entity;
using LTJ.Common.Generic;
using LTJ.Cache;

namespace LTJ.FrameWork
{
    /// <summary>
    /// 规则操作
    /// </summary>
    public class Rules
    {
        /// <summary>
        /// 增设规则
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="oid">企业ID</param>
        /// <param name="rtype">规则类型</param>
        /// <param name="_20GP">20GP价格</param>
        /// <param name="_40GP">40GP价格</param>
        /// <param name="_40HQ">40HQ价格</param>
        /// <param name="rid">航线ID</param>
        /// <param name="port">港口</param>
        public static int CreateRules(int uid, int oid, int rtype, string _20GP, string _40GP, string _40HQ, int rid, string port, string seacode)
        {
            if (ExistRule(uid, oid, rtype, rid, port, seacode)) return -1;

            return Data.DataProvider.Rules.CreateRules(uid, oid, rtype, _20GP, _40GP, _40HQ, rid, port, seacode);
        }

        /// <summary>
        /// 是否存在规则信息
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="oid">企业ID</param>
        /// <param name="rtype">规则类型</param>
        /// <param name="rid">航线ID</param>
        /// <param name="port">港口</param>
        public static bool ExistRule(int uid, int oid, int rtype, int rid, string port, string seacode)
        {
            return Data.DataProvider.Rules.ExistRule(uid, oid, rtype, rid, port, seacode);
        }

        /// <summary>
        /// 获取规则数量
        /// </summary>
        public static int GetRulesCount(string condition)
        {
            return Data.DataProvider.Rules.GetRulesCount(condition);
        }

        /// <summary>
        /// 规则基本信息分页操作（DataTable对象)
        /// </summary>
        public static DataTable GetRulesByPage(int pageindex, int pagesize, string condition, string ordercolumn, string ordertype)
        {
            return Data.DataProvider.Rules.GetRulesByPage(pageindex, pagesize, condition, ordercolumn == "" ? "id" : ordercolumn, ordertype == "" ? "desc" : ordertype);
        }

        /// <summary>
        /// 批量删除操作
        /// </summary>
        public static void DeleteRulesByIds(string ids)
        {
            Data.DataProvider.Rules.DeleteRulesByIds(ids);
        }

        /// <summary>
        /// 获取规则实体
        /// </summary>
        public static RuleInfo GetRuleInfoByID(int id)
        {
            return Data.DataProvider.Rules.GetRuleInfoByID(id);
        }

        /// <summary>
        /// 修改规则价格设置
        /// </summary>
        /// <param name="price1"></param>
        /// <param name="price2"></param>
        /// <param name="price3"></param>
        public static void UpdateRulePrice(int id, string price1, string price2, string price3)
        {
            Data.DataProvider.Rules.UpdateRulePrice(id, price1, price2, price3);
        }

        /// <summary>
        /// 其他公司航线价格分页操作
        /// </summary>
        public static DataTable GetRoutePriceRuleByPage(int type, int uid, int cid, int pageindex, int pagesize, string condition, string ordercolumn, string ordertype)
        {
            return Data.DataProvider.Rules.GetRoutePriceRuleByPage(type, uid, cid, pageindex, pagesize, condition, ordercolumn == "" ? "id" : ordercolumn, ordertype == "" ? "desc" : ordertype);
        }

        /// <summary>
        /// 其他公司线路价格规则批量调整
        /// </summary>
        /// <param name="ids">线路ID集合</param>
        public static void UpdateRules(string ids, int uid, string price1, string price2, string price3)
        {
            Data.DataProvider.Rules.UpdateRules(ids, uid, price1, price2, price3);
        }
        /// <summary>
        /// 其他公司线路价格规则批量调整（用户批量）
        /// </summary>
        public static void UpdateRules(string ids, string uids, string price1, string price2, string price3)
        {
            Data.DataProvider.Rules.UpdateRules(ids, uids, price1, price2, price3);
        }

        /// <summary>
        /// 获取用户规则查询条件
        /// </summary>
        /// <param name="ruletype">规则类型</param>
        /// <param name="bcode">船公司</param>
        /// <param name="tcode">航线</param>
        /// <param name="ports">港口</param>
        /// <param name="lname">会员名/公司名</param>
        public static string GetUserRuleSearchCondition(int ruletype, string bcode, string tcode, string ports, string lname)
        {
            return GetRuleSearchCondition(ruletype, bcode, tcode, ports, lname, 1);
        }

        /// <summary>
        /// 获取公司规则查询条件
        /// </summary>
        /// <param name="ruletype">规则类型</param>
        /// <param name="bcode">船公司</param>
        /// <param name="tcode">航线</param>
        /// <param name="ports">港口</param>
        /// <param name="lname">会员名/公司名</param>
        public static string GetCorpRuleSearchCondition(int ruletype, string bcode, string tcode, string ports, string lname)
        {
            return GetRuleSearchCondition(ruletype, bcode, tcode, ports, lname, 2);
        }
        /// <summary>
        /// 获取大赢家系统规则查询条件
        /// </summary>
        /// <param name="ruletype">规则类型</param>
        /// <param name="bcode">船公司</param>
        /// <param name="tcode">航线</param>
        /// <param name="ports">港口</param>
        /// <param name="lname">会员名/公司名</param>
        public static string GetSysRuleSearchCondition(int ruletype, string bcode, string tcode, string ports, string lname)
        {
            return GetRuleSearchCondition(ruletype, bcode, tcode, ports, lname, 3);
        }

        /// <summary>
        /// 获取规则查询条件
        /// </summary>
        /// <param name="ruletype">规则类型</param>
        /// <param name="bcode">船公司</param>
        /// <param name="tcode">航线</param>
        /// <param name="ports">港口</param>
        /// <param name="lname">会员名/公司名</param>
        public static string GetRuleSearchCondition(int ruletype, string bcode, string tcode, string ports, string lname, int type)
        {
            string returnstr = "";

            if (ruletype > 0)
            {
                if (type == 3)
                {

                }
                else
                {
                    if (ruletype == 1) returnstr = "rtype = 0 AND rid = 0";
                    if (ruletype == 2) returnstr = "rtype = 0 AND rid > 0";
                    if (ruletype == 3) returnstr = "rtype = 1";
                    if (ruletype == 4) returnstr = "rtype = 2";
                }
            }
            if (bcode != "")
            {
                if (returnstr != "") returnstr += " AND ";
                returnstr += "bcode = '" + bcode + "'";
            }
            if (tcode != "")
            {
                if (returnstr != "") returnstr += " AND ";
                returnstr += "tcode = '" + tcode + "'";
            }
            if (ports != "" && Utils.IsSafeSqlString(ports))
            {
                if (returnstr != "") returnstr += " AND ";
                returnstr += "port = '" + Utils.ChkSQL(ports) + "'";
            }
            if (lname != "" && Utils.IsSafeSqlString(lname))
            {
                if (returnstr != "") returnstr += " AND ";
                if (type == 1) returnstr += "lname like '%" + Utils.ChkSQL(lname) + "%'";
                if (type == 2) returnstr += "cname like '%" + Utils.ChkSQL(lname) + "%'";
            }

            return returnstr;
        }
    }
}
